<template>
	<view>
		<view class="cell-item">
			<view class="cell-item-title">姓名</view>
			<input type="text" :disabled="isDisabled" v-model="real_name" placeholder="请输入真实姓名，需与认证信息一致">
		</view>
		<view class="cell-item" v-show="manTitle == '银行卡'">
			<view class="cell-item-title">卡号</view>
			<input type="text" :disabled="isDisabled" v-model="bank_account" placeholder="请输入卡号">
		</view>
		<view class="cell-item" v-show="manTitle == '银行卡'">
			<view class="cell-item-title">开户行</view>
			<input type="text" :disabled="isDisabled" v-model="bank_name" placeholder="请输入开户行">
		</view>
		<view class="cell-item" v-show="manTitle == '微信' || manTitle == '支付宝'">
			<view class="cell-item-title">{{manTitle}}收款二维码</view>
		</view>
		<view class="up-img" v-show="manTitle == '微信' || manTitle == '支付宝'">
			<!-- <image v-if="!image" src="/static/me/photo.png" mode="" @click="choiceImg"></image> -->
			<image v-if="isDisabled == false" :src="images ? $IMG_URL+ images : '/static/me/photo.png'"
				@click="choiceImg" mode=""></image>
			<image v-else @click="getimgPreview($IMG_URL+ images)" :src="$IMG_URL+ images" mode=""></image>
			<view v-show="isDisabled == false" class="up-tet">上传二维码</view>
		</view>
		<view v-show="isDisabled == false" class="detail-bottom-btn" @click="bindMsg">
			确定
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isDisabled: false, //查看收款信息不能编辑
				images: '',
				manTitle: '',
				accountInfo: {},
				type: 20, //10=支付宝,20=微信,30=银行卡
				seller_id: 0, //平台 1 普通用户只可看
				user_id: '',
				real_name: '',
				bank_account: '',
				bank_name:''
			}
		},
		onLoad(options) {
			console.log(options, 111)
			
			this.isDisabled = options.title == '查看收款信息' ? true : false
			this.seller_id = options.seller_id
			// this.accountInfo = JSON.parse(options.itemInfo)
			this.real_name = JSON.parse(options.itemInfo).real_name
			this.bank_account = JSON.parse(options.itemInfo).bank_account
			this.bank_name = JSON.parse(options.itemInfo).bank_name
			this.images = JSON.parse(options.itemInfo).image
			this.manTitle = options.manTitle
			this.type = options.manTitle == '微信' ? 20 : options.manTitle == '支付宝' ? 10 : 30
			// this.user_id = options.user_id == 'undefined' ? 0 : options.user_id
			uni.setNavigationBarTitle({
				title: options.manTitle
			})
			if(options.title == '收款管理'){
				this.init()
			}
		},
		methods: {
			// 图片放大
			getimgPreview(img) {
				let imgList = []
				imgList.push(img)
				uni.previewImage({
					indicator: "number",
					loop: true,
					urls: imgList
				})
			},
			init() {
				if (this.seller_id == 0) {
					this.$http('other.getAccountDetailsByAdmin', {
						type: this.type //10=支付宝,20=微信,30=银行卡
					}, '获取中...').then(res => {
						if (res.code == 1) {
							if (res.data) {
								this.accountInfo = res.data
								this.real_name = res.data.real_name
								this.bank_account = res.data.bank_account
								this.bank_name = res.data.bank_name
								this.images = res.data.image
							}
						}
					})

				} else {
					this.$http('other.getAccountDetails', {
						type: this.type, //10=支付宝,20=微信,30=银行卡
						user_id: this.user_id //	如果传递的话查询的时候传递的id 如果不传的话，查询的是自己id sellid
					}, '获取中...').then(res => {
						if (res.code == 1) {
							if (res.data) {
								this.accountInfo = res.data
								this.accountInfo = res.data
								this.real_name = res.data.real_name
								this.bank_account = res.data.bank_account
								this.bank_name = res.data.bank_name
								this.images = res.data.image
							}
						}
					})
				}
			},
			// 绑定信息
			bindMsg() {
				if (!this.real_name) {
					uni.showToast({
						icon: 'none',
						title: '请输入真实姓名，需与认证信息一致'
					})
					return
				}
				if (this.manTitle == '微信' || this.manTitle == '支付宝') {
					if (!this.images) {
						uni.showToast({
							icon: 'none',
							title: '上传二维码'
						})
						return
					}
				}
				if (this.manTitle == '银行卡' && !this.bank_account) {
					uni.showToast({
						icon: 'none',
						title: '请输入卡号'
					})
					return
				}
				if (this.manTitle == '银行卡' && !this.bank_name) {
					uni.showToast({
						icon: 'none',
						title: '请输入开户行'
					})
					return
				}
				let data = {}
				if (this.manTitle == '微信' || this.manTitle == '支付宝') {
					data.real_name = this.real_name
					data.type = this.type
					data.image = this.images
				} else {
					data.real_name = this.real_name
					data.type = this.type
					data.bank_account = this.bank_account
					data.bank_name = this.bank_name
				}
				this.$http('other.editAccount', data, '获取中...').then(res => {
					if (res.code == 1) {
						uni.showToast({
							icon: 'none',
							title: '绑定成功'
						})
						setTimeout(() => {
							this.init()
						}, 2000)
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			// 选择图片操作
			choiceImg() {
				uni.showActionSheet({
					itemList: ['拍照', '从手机相册选择'],
					success: res => {
						switch (res.tapIndex) {
							case 0:
								uni.chooseImage({
									count: 1, //默认9
									sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
									sourceType: ['camera'], //从相册选择
									success: ress => {
										this.uploadUserImg(ress.tempFilePaths[0]);
									}
								});
								break;
							case 1:
								uni.chooseImage({
									count: 1, //默认9
									sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
									sourceType: ['album'], //从相册选择
									success: ress => {
										this.uploadUserImg(ress.tempFilePaths[0])
									}
								});
								break;
							default:
								break;
						}
					},
					fail: function(res) {
						console.log(res.errMsg);
					}
				});
			},
			// 上传
			uploadUserImg(blob) {
				let that = this
				uni.showLoading({
					title: '上传中...'
				});
				uni.uploadFile({
					// 	Authorization: 'Bearer' + ' ' + uni.getStorageSync('token')
					url: that.$API_URL + '/common/upload',
					filePath: blob,
					name: 'file',
					header: {
						'token': uni.getStorageSync('token')
					},
					success: res => {
						uni.hideLoading();
						let imgData = JSON.parse(res.data)
						that.images = imgData.data.url
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #F6F7F9;
	}

	.cell-item {
		display: flex;
		background-color: #fff;
		padding: 20rpx;
		border-bottom: 3rpx solid #EEEEEE;

		.cell-item-title {
			font-weight: 400;
			font-size: 32rpx;
			color: #333333;
			margin: 20rpx 20rpx 0 0;
		}

		input {
			width: 83%;
			height: 80rpx;
		}

	}

	.up-img {
		width: 100%;
		height: 260rpx;
		background-color: #fff;
		padding-top: 20rpx;

		image {
			width: 160rpx;
			height: 160rpx;
			margin-left: 20rpx;
		}

		.up-tet {
			font-weight: 400;
			font-size: 28rpx;
			color: #666666;
			margin: 20rpx 0 0 35rpx;
		}
	}

	.detail-bottom-btn {
		position: fixed;
		z-index: 200;
		bottom: 30rpx;
		width: 686rpx;
		height: 88rpx;
		background: linear-gradient(90deg, #6AAE2B 0%, #1B7E38 100%);
		font-weight: 500;
		font-size: 32rpx;
		color: #FFFFFF;
		line-height: 88rpx;
		text-align: center;
		left: 50%;
		transform: translate(-50%);
		border-radius: 50rpx;
	}
</style>